﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class Credit_Teacher_RuleSettings_SettingEval : System.Web.UI.Page
{
    protected int currHistoryId = 0,currCauseId = 0  ;
    protected List<StuCauseEvalType> listType = new List<StuCauseEvalType>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string s = Request.Params["historyId"];
            int.TryParse(s, out currHistoryId);

            s = Request.Params["causeId"];
            int.TryParse(s, out currCauseId);

          //  listType =  GetTypeListByCauseId(currHistoryId, currCauseId);
            //repeateType.DataSource = GetTypeListByCauseId(currHistoryId, currCauseId);
            //repeateType.DataBind();

            //repeateCause.DataSource = SelectSchoolCuase();
            //repeateCause.DataBind();
        }
    }


    protected void btnTypeBind_Click(object sender, EventArgs e)
    {
        int historyId = 0, causeId = 0;
        string s = "";

        s = txtHistoryId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out historyId);

        s = txtCauseId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out causeId);

        currCauseId = causeId;
        currHistoryId = historyId;

        //listType = GetTypeListByCauseId(currHistoryId, currCauseId);
        //SetType();
        //repeateType.DataSource = GetTypeListByCauseId(historyId, causeId);
        //repeateType.DataBind();

   
    }
    protected void btnItemBind_Click(object sender, EventArgs e)
    {
        int historyId = 0,causeId = 0, typeId = 0,schoolYear = 0,term = 0;;
        string s = "";

        s = txtTypeId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out typeId);

        s = txtHistoryId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out historyId);

        s = txtCauseId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out causeId);
 

        schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;

        Hashtable table = new Hashtable();
        table.Add("SchoolMark", Common.GetConstSchoolMark());
        table.Add("TypeID", typeId);
 

        IStuCauseEvalHistoryLogic logic = Container<IStuCauseEvalHistoryLogic>.Instance;
        List<VStuCauseEvalHistoryTypeItem> list = new List<VStuCauseEvalHistoryTypeItem>();
        list = (List<VStuCauseEvalHistoryTypeItem>)logic.FindWithTypeItme(Common.GetConstSchoolMark(), schoolYear, term, historyId, causeId, typeId);

        currCauseId = causeId;
        currHistoryId = historyId;

        Repeater1.DataSource = list;
        Repeater1.DataBind();
    }

    /// <summary>
    /// 获取causeEvalItem列表
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<StuCauseEvalType> GetTypeListByCauseId(int historyId,int causeId)
    {
        Hashtable table = new Hashtable();
        table.Add("SchoolMark", Common.GetConstSchoolMark());
        table.Add("HistoryID", historyId);
        table.Add("CauseID", causeId);

        List<StuCauseEvalType> list = new List<StuCauseEvalType>();

        IStuCauseEvalTypeLogic logic = Container<IStuCauseEvalTypeLogic>.Instance;
        list = (List<StuCauseEvalType>)logic.FindAll(table);
        return list;
    }

    ///// <summary>
    ///// 获取学校的所有有效课程
    ///// </summary>
    ///// <returns></returns>
    //private List<GlobalCauseInfo> SelectSchoolCuase()
    //{
    //    IGlobalCauseInfoLogic logic = Container<IGlobalCauseInfoLogic>.Instance;
    //    List<GlobalCauseInfo> list = ( List<GlobalCauseInfo>) logic.SelectSchoolCause(Common.GetConstSchoolMark(), 1);
    //    return list;
    //}


    /// <summary>
    /// 获取学分评估的类型和规则信息
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <param name="typeId"></param>
    /// <returns></returns>
    private List<VStuCauseEvalHistoryTypeItem> GetEvalList(int historyId, int causeId, int typeId)
    {
        int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        int term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;
        string schoolMark = Common.GetConstSchoolMark(); 

        IStuCauseEvalHistoryLogic logic = Container<IStuCauseEvalHistoryLogic>.Instance;
        List<VStuCauseEvalHistoryTypeItem> list = (List<VStuCauseEvalHistoryTypeItem>)logic.FindWithTypeItme(schoolMark, schoolYear, term, historyId, causeId, typeId);
        return list;
    }

  
    #region 类型的增删改

    /// <summary>
    /// 验证类型名是否已被使用
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <param name="typeName"></param>
    /// <returns>0-未被使用，大于0被使用</returns>
    [System.Web.Services.WebMethod]
    public static int ValidateTypeName(int historyId,int causeId,string typeName)
    {
        int n = 0;
        IStuCauseEvalTypeLogic logic = Container<IStuCauseEvalTypeLogic>.Instance;
        n = logic.ValidateTypeName(Common.GetConstSchoolMark(),historyId, causeId, typeName);
        return n;
    }
 
    [System.Web.Services.WebMethod]
    public static int AddType(int historyId,int causeId,string typeName)
    {
        IStuCauseEvalTypeLogic logic = Container<IStuCauseEvalTypeLogic>.Instance;  
        int n = 0;
        if (historyId > 0)
        {
            //已存在于history表的
            n = logic.InsertType(Common.GetConstSchoolMark(), historyId, causeId, typeName);
        }
        else
        {
            //需重新创建history之后再创建type的
            int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
            int term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;

            n = logic.InsertTypeHistory(Common.GetConstSchoolMark(), causeId, typeName, schoolYear, term, Common.GetUserInfo().UID);
        }  
        return n;
    }

    
    [System.Web.Services.WebMethod]
    public static int DelTypeById(int historyId,int typeId)
    {
        int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        int term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;
        IStuCauseEvalTypeLogic logic = Container<IStuCauseEvalTypeLogic>.Instance;
        return logic.DeleteById(Common.GetConstSchoolMark(),historyId,typeId,schoolYear,term);
    }

    [System.Web.Services.WebMethod]
    public static int UpdateType(int typeId,string typeName)
    {
        IStuCauseEvalTypeLogic logic = Container<IStuCauseEvalTypeLogic>.Instance;
        return  logic.UpdateTypeName(Common.GetConstSchoolMark(), typeName, typeId);
    }

    #endregion

    protected string GetTypeOpt(int type)
    {
         if(type < 1 || type > 2)
             return "";

        string s = "";
        if(type == 1)
        {
            s = "<option value='1' selected='selected'>计分</option>" +
            "  <option value='2'>计次</option>";
        }
        if(type == 2)
        {
            s = "<option value='1'>计分</option>" +
           "  <option value='2' selected='selected'>计次</option>";
        }
        return s;
    }

    protected string GetOperatorOpt(int nOperator)
    {
        string s = "";
        if (nOperator == 1)
        {
            s = "<option value='1' selected='selected'>加分</option>" +
              "<option value='-1'>减分</option>";
        }
        else
        {
            s = "<option value='1'>加分</option>" +
             "<option value='-1' selected='selected'>减分</option>";
        }
        return s;
    }

    protected string GetDisplay(int itemType)
    {
        string s = "";
        if (itemType == 1)
        {
            s = "none";
        }
        else
        {
            s = "block";
        }
        return s;
    }



    [System.Web.Services.WebMethod]
    public static int SaveCauseEvalItem(VStuCauseEvalHistoryTypeItem[] list, VStuCauseEvalHistoryTypeItem[] delIds)
    {
        List<VStuCauseEvalHistoryTypeItem> listDel = new List<VStuCauseEvalHistoryTypeItem>();
        List<VStuCauseEvalHistoryTypeItem> listAdd = new List<VStuCauseEvalHistoryTypeItem>();
        foreach (VStuCauseEvalHistoryTypeItem item in list)
        {
            listAdd.Add(item); 
            if (item.ItemID > 0)
                listDel.Add(item); 
        }

        if (delIds.Length > 0)
        {
            foreach (VStuCauseEvalHistoryTypeItem item2 in delIds)
            {  
                listDel.Add(item2);
            }
        }

        IStuCauseEvalItemLogic logic = Container<IStuCauseEvalItemLogic>.Instance;
        int n = logic.InsertCauseItem(Common.GetConstSchoolMark(),listDel,listAdd);
        
        return n;
    }

    #region 绑定数据后 需要处理的前台效果 比如选中状态

    //做个点击效果
    protected void SetTypeArr()
    {
        Response.Write("currTypeId = 0;");
        Response.Write("currTypeName = \"\";");
        Response.Write("var ul = document.getElementById('ul');");
        Response.Write("var listA = document.getElementsByTagName('a');");
        Response.Write("if(listA.length > 0)");
        Response.Write("{");
        Response.Write("    for(var i =0;i < listA.length;i++)");
        Response.Write("    {");
        Response.Write("        if(i==0)");
        Response.Write("        {");
        Response.Write("            listA[i].click();");
        Response.Write("            currTypeId = listA[i].id;");
        Response.Write("            currTypeName = listA[i].Text;");
        Response.Write("        }");
        Response.Write("    }");
        Response.Write("}");

    }

    #endregion
}
